#include <GL/glew.h>
#include <SDL2/SDL.h>
#include <assert.h>
#include <stdint.h>
#include <math.h>

#include "glUtils.h"
#include "initSDL.h"

const uint32_t width = 640, height = 480;
GLfloat vVertices[6 * 2];
GLfloat vColors[] = {1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0,
                     1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0};
GLushort idx[] = {0, 1, 2, 3, 4, 5};

int main(int argc, char *argv[]) {
    int run = 1;
    SDL_Window *win = InitSDLWindow("lines", width, height);
    GLuint program = InitShader("triangle");
    if (!program) {
        return 0;
    }
    for(int i = 0; i < 6; i++) {
        float a = i / 6.0 * 3.141592 * 2;
        float * point = &vVertices[i * 2];
        point[0] = cos(a);
        point[1] = sin(a);
    }
    glClearColor(0.8, 0.8, 0.8, 1.0);
    InitAttrArrayBuffer(program, vVertices, sizeof(vVertices), 2, "vPosition");
    InitAttrArrayBuffer(program, vColors, sizeof(vColors), 3, "vColor");
    GLuint idx_buf = InitElementsBuffer(idx, sizeof(idx));
    glUseProgram(program);
    while (run) {
        SDL_Event event;
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                run = 0;
            }
        }

        glClear(GL_COLOR_BUFFER_BIT);
        glLineWidth(5);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, idx_buf);
        glDrawElements(GL_LINES, 6, GL_UNSIGNED_SHORT, 0);
        SDL_GL_SwapWindow(win);
    }
    SDL_DestroyWindow(win);
    return 0;
}